package com.wondedDream.service.impl;

import com.wondedDream.model.MockExamQuestion;
import com.wondedDream.model.UserMockExamQuestionRecord;
import com.wondedDream.repository.MockExamQuestionRepository;
import com.wondedDream.repository.UserMockExamQuestionRecordRepository;
import com.wondedDream.service.MockExamService;
import com.wondedDream.service.UserMockExamQuestionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional
public class UserMockExamQuestionRecordServiceImpl implements UserMockExamQuestionRecordService {
    @Autowired
    private UserMockExamQuestionRecordRepository userMockExamQuestionRecordRepository;

    @Autowired
    private MockExamQuestionRepository mockExamQuestionRepository;

    @Override
    public UserMockExamQuestionRecord saveUserMockExamQuestionRecord(UserMockExamQuestionRecord record) {
        return userMockExamQuestionRecordRepository.save(record);
    }

    @Override
    public UserMockExamQuestionRecord getUserMockExamQuestionRecordById(Integer recordId) {
        return userMockExamQuestionRecordRepository.findById(recordId).orElse(null);
    }

    @Override
    public List<UserMockExamQuestionRecord> getUserMockExamQuestionRecordsByUserMockExamRecord(Integer userMockExamRecordId) {
        return userMockExamQuestionRecordRepository.findByUserMockExamRecordId(userMockExamRecordId);
    }

    @Override
    public List<UserMockExamQuestionRecord> getUserMockExamQuestionRecordsByMockExamQuestion(String mockExamQuestionId) {
        return userMockExamQuestionRecordRepository.findByMockExamQuestionId(mockExamQuestionId);
    }

    @Override
    public List<UserMockExamQuestionRecord> getAllUserMockExamQuestionRecords() {
        return userMockExamQuestionRecordRepository.findAll();
    }

    @Override
    public void deleteUserMockExamQuestionRecord(Integer recordId) {
        userMockExamQuestionRecordRepository.deleteById(recordId);
    }

    @Override
    public List<MockExamQuestion>  getUserMockExamQuestionRecordByIdAndOpenid(Integer userMockExamRecordId, String userOpenId) {
        //答题记录
        List<UserMockExamQuestionRecord>  userMockExamQuestionRecords = userMockExamQuestionRecordRepository.getUserMockExamQuestionRecordByIdAndOpenid(userMockExamRecordId,userOpenId);
        //问题记录
        List<MockExamQuestion> mockExams = mockExamQuestionRepository.findByMockExamId(userMockExamRecordId);
        List<MockExamQuestion> mockExamQuestions = updateMockExams(userMockExamQuestionRecords, mockExams);
        return mockExamQuestions;
    }

    @Override
    public List<MockExamQuestion> getUserErrorMockExamQuestionRecordByIdAndOpenid(Integer userMockExamRecordId, String userOpenId) {
        //答题记录
        List<UserMockExamQuestionRecord>  userMockExamQuestionRecords = userMockExamQuestionRecordRepository.getUserMockExamQuestionRecordByIdAndOpenid(userMockExamRecordId,userOpenId);
        //问题记录
        List<MockExamQuestion> mockExams = mockExamQuestionRepository.findByMockExamId(userMockExamRecordId);
        return getErrorMockExams(userMockExamQuestionRecords, mockExams);
    }


    private  List<MockExamQuestion> getErrorMockExams(List<UserMockExamQuestionRecord> userMockExamQuestionRecords, List<MockExamQuestion> mockExams) {
        // 将 mockExams 转换为以 id 为键，MockExamQuestion 为值的映射
        List<MockExamQuestion> mockExamList = mockExams.stream()
                .collect(Collectors.toMap(MockExamQuestion::getId, q -> q))
                .values()
                .stream()
                .collect(Collectors.toList());
        List<UserMockExamQuestionRecord> incorrectRecords = userMockExamQuestionRecords.stream()
                .filter(record ->!record.getIsCorrect())
                .collect(Collectors.toList());
        // 遍历 userMockExamQuestionRecords，更新对应的 MockExamQuestion
        for (UserMockExamQuestionRecord record : incorrectRecords) {
            Integer mockExamId = record.getMockExam().getId();
            for (MockExamQuestion question : mockExamList) {
                if (question.getId().equals(mockExamId)) {
                    question.setAnswer(record.getAnswer());
                    question.setIsCorrect(record.getIsCorrect());
                }
            }
        }
        return mockExamList;
    }

    private  List<MockExamQuestion> updateMockExams(List<UserMockExamQuestionRecord> userMockExamQuestionRecords, List<MockExamQuestion> mockExams) {
        // 将 mockExams 转换为以 id 为键，MockExamQuestion 为值的映射
        List<MockExamQuestion> mockExamList = mockExams.stream()
                .collect(Collectors.toMap(MockExamQuestion::getId, q -> q))
                .values()
                .stream()
                .collect(Collectors.toList());
        // 遍历 userMockExamQuestionRecords，更新对应的 MockExamQuestion
        for (UserMockExamQuestionRecord record : userMockExamQuestionRecords) {
            Integer mockExamId = record.getMockExam().getId();
            for (MockExamQuestion question : mockExamList) {
                if (question.getId().equals(mockExamId)) {
                    question.setAnswer(record.getAnswer());
                    question.setIsCorrect(record.getIsCorrect());
                }
            }
        }
        return mockExamList;
    }


}